$(function() {
	// 1 初始化分页信息
	initEventPage();
	getInitData();
	getRejectData();
});
$("#reportImgAdress").error(function() { 
	$(this).attr("src", "../img/nopicture.png"); 
	$(this).error=null;
	}); 
var level;
var serverData = {};
var serverDataDeal={};
var serverTable;
// 8 撤销查询
jQuery('.resetBtnLeft').on('click', function() {
	jQuery('#searchLeft').val('');
	serverData.key = '';
	serverTable.draw();
});
// 7 点击查询按钮
$(".searchBtnLeft").click(function() {
	serverData.key = jQuery("#searchLeft").val();
	serverTable.draw();
});
function dealEventRegister(data){
	window.location = window.rootPath
	+ '/ui/eventRegister?eventReportId='+data.eventId;
}
// 6 点击编辑按钮打开模态框
function dealModule(data) {
	jQuery('#eventReportModal').modal('show');
	$("#identify").val(data.id);
	$("#reportContent").val(data.reportContent);
	$("#reportMemo").val(data.reportMemo);
	$("#reportAccTime").val(moment(data.reportAccTime).format('YYYY/MM/DD HH:mm'));
	
	if(data.reportUser){
		$("#reportUser").val(data.reportUser.name);
		$("#reportUserPhone").val(data.reportUser.contactType);
	}
	
	$("#reportImgAdress").attr("src",rootPath+"/"+data.reportImgAdress);
	
	$("#reportAdress").val(data.reportAdress);
	
	if(data.reportEventToNetGrid)
	$("#reportEventToNetGrid").val(data.reportEventToNetGrid.gridName);
	
	if(data.reportEventVariable)
	$("#reportEventVariable").val(data.reportEventVariable.title);
	
	if(data.reportEventAttrVariable)
		$("#reportEventAttrVariable").val(data.reportEventAttrVariable.id);
	if(data.reportEventType)
		$("#reportEventType").val(data.reportEventType.eventName);
};
//待分配列表
var serverDataForInit = {};
var serverTableForInit;
function getInitData() {
	serverTableForInit = jQuery('#dataTableForInit').DataTable({
        "serverSide": true,
        "ordering": false,
        "ajax": {
            "dataType": "json",
            "type": "get",
            "cache": false,
            "url":window.rootPath+ '/ui/eventRegister/getpageForSelfInit',
            "data": function (d) {
                return $.extend({}, d, serverDataForInit);
            }
        },
        columns: [
            {
            data: null
            },
            {
                data: null,
                defaultContent: "",
                render: function (data, type, full, meta) {
                	
                	var operate ='<button type="button" class="btn btn-sm btn-info detailBtn" title="详情"><i class="fa fa-edit"></i></button>';
                	if(data.registerEventState){
                		if(data.registerEventState.title=='初始化'){
                    		operate+='<button type="button" class="btn btn-sm btn-info submitBtn" title="发布"><i class="fa fa-bullhorn"></i></button>';
                    	}else if(data.registerEventState.title=='已分配'){
                    		operate+='<button type="button" class="btn btn-sm btn-info backBtn" title="收回"><i class="fa fa-back"></i></button>';
                    	}else if(data.registerEventState.title=='已退回'){
                    		operate+='<button type="button" class="btn btn-sm btn-info rejectBackBtn" title="确认退回"><i class="fa fa-recycle"></i></button>';
                    	}
                	}
                	
                    return operate;
                }
            },
            {
            	data: 'registerEventName',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'registerEventContent',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'registerEventReportTime',
                render: function (data, type, full, meta) {
                    if (data)
                        return moment(data).format('YYYY/MM/DD HH:mm');
                    else
                        return '';
                }
            },
            {
                data: 'registerEventIdentify',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'registerEventType.eventName',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            }/*,
            {
                data: 'registerEventEvaluateLevel.gradingName',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'registerEventSeriousLevel.title',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },{
				data : "registerEventImgAdress",
				render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return '<img width="50%" src="'+window.rootPath+"/"+data+'" />';
					}
				}

			}*/,
            {
                data: 'registerEventAdress',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'registerEventToNetGrid.gridName',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            }/*,
            {
                data: 'registerEventState.title',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            }*/
          ],
        "drawCallback": function (oSettings) {
            var k = 0;
            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

            for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                k++;
            }

            serverTableForInit.off('click', 'tr');
            serverTableForInit.on('click', 'tr', function (event) {
                event = event || window.event;
                var target = event.target || event.scrElement;
                var data = serverTableForInit.row(this).data();
                if (target.className.indexOf('fa-bullhorn') != -1 ||target.className.indexOf('submitBtn') != -1) {
                	submitBtnQ(data);
                }
                if (target.className.indexOf('fa-back') != -1 ||target.className.indexOf('backBtn') != -1) {
                	backBtnQ(data);
                }
                
                if (target.className.indexOf('fa-edit') != -1 ||target.className.indexOf('detailBtn') != -1) {
                    showDetail(data);
                }
                if (target.className.indexOf('fa-recycle') != -1 ||target.className.indexOf('rejectBackBtn') != -1) {
                	rejectBackBtnQ(data);
                }
                return;
            });
        }
    });
};
//已退回列表
var serverDataForReject = {};
var serverTableForReject;
function getRejectData() {
	serverTableForReject = jQuery('#dataTableForRejectBack').DataTable({
        "serverSide": true,
        "ordering": false,
        "ajax": {
            "dataType": "json",
            "type": "get",
            "cache": false,
            "url":window.rootPath+ '/ui/eventRegister/getpageForSelfReject',
            "data": function (d) {
                return $.extend({}, d, serverDataForReject);
            }
        },
        columns: [
            {
            data: null
            },
            {
                data: null,
                defaultContent: "",
                render: function (data, type, full, meta) {
                	
                	var operate ='<button type="button" class="btn btn-sm btn-info detailBtn" title="详情"><i class="fa fa-edit"></i></button>';
                	if(data.registerEventState){
                		if(data.registerEventState.title=='初始化'){
                    		operate+='<button type="button" class="btn btn-sm btn-info submitBtn" title="发布"><i class="fa fa-bullhorn"></i></button>';
                    	}else if(data.registerEventState.title=='已分配'){
                    		operate+='<button type="button" class="btn btn-sm btn-info backBtn" title="收回"><i class="fa fa-back"></i></button>';
                    	}else if(data.registerEventState.title=='已退回'){
                    		operate+='<button type="button" class="btn btn-sm btn-info rejectBackBtn" title="确认退回"><i class="fa fa-recycle"></i></button>';
                    	}
                	}
                	
                    return operate;
                }
            },
            {
            	data: 'registerEventName',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'registerEventContent',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'registerEventReportTime',
                render: function (data, type, full, meta) {
                    if (data)
                        return moment(data).format('YYYY/MM/DD HH:mm');
                    else
                        return '';
                }
            },
            {
                data: 'registerEventIdentify',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'registerEventType.eventName',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            }/*,
            {
                data: 'registerEventEvaluateLevel.gradingName',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'registerEventSeriousLevel.title',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },{
				data : "registerEventImgAdress",
				render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return '<img width="50%" src="'+window.rootPath+"/"+data+'" />';
					}
				}

			}*/,
            {
                data: 'registerEventAdress',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'registerEventToNetGrid.gridName',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            }/*,
            {
                data: 'registerEventState.title',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
					}
				}
            }*/
          ],
        "drawCallback": function (oSettings) {
            var k = 0;
            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

            for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                k++;
            }

            serverTableForReject.off('click', 'tr');
            serverTableForReject.on('click', 'tr', function (event) {
                event = event || window.event;
                var target = event.target || event.scrElement;
                var data = serverTableForReject.row(this).data();
                if (target.className.indexOf('fa-bullhorn') != -1 ||target.className.indexOf('submitBtn') != -1) {
                	submitBtnQ(data);
                }
                if (target.className.indexOf('fa-back') != -1 ||target.className.indexOf('backBtn') != -1) {
                	backBtnQ(data);
                }
                
                if (target.className.indexOf('fa-edit') != -1 ||target.className.indexOf('detailBtn') != -1) {
                    showDetail(data);
                }
                if (target.className.indexOf('fa-recycle') != -1 ||target.className.indexOf('rejectBackBtn') != -1) {
                	rejectBackBtnQ(data);
                }
                return;
            });
        }
    });
};
var backBtnQ=function(o){
	jQuery('#backModal').modal('show');
	$("#backId").val(o.id);
};
var showDetail=function(o){
	if(!o) return;
	window.location = window.rootPath
	+ '/ui/eventRegister?eventReportIdentify='+o.registerEventToReportEvent.id+"&eventRegisterIdentify="+o.id;
};
var rejectBackBtnQ=function(o){
	 $("#rejectBackId").val(o.id);
	 
	jQuery('#rejectBackModal').modal('show');
	$.ajax({
		url : rootPath + "/ui/eventRegister/getRejectBackReason?id="
				+ $("#rejectBackId").val(),
		dataType : 'json',
		type : 'get',
		contentType : "application/json",
		success : function(result) {
			if (result.data.code <= 0) {
				message("获取退回原因失败!");
			} else {
				if(result.data.reasonStr){
					$("#rejectBackReason").val(result.data.reasonStr.eventTaskMemo);
					if(result.data.reasonStr.eventTaskUser){
						$("#rejectBackPerson").val(result.data.reasonStr.eventTaskUser.name);
					}
				}
			}
		}
	});
};
$(".saverejectBackBtn").on(
		'click',
		function() {
			var data = {
				"id" : $("#rejectBackId").val(),
				"rejectBackMemo" : $("#rejectBackMemo").val(),
			}
			$.ajax({
				url : rootPath + "/ui/eventRegister/rejectBackEventRegister",
				dataType : 'json',
				data : JSON.stringify(data),
				type : 'post',
				contentType : "application/json",
				success : function(result) {
					if (result.data.code <= 0) {
						message("操作失败!");
					} else {
						message(result.data.rejectBackStr);
					}
					jQuery('#rejectBackModal').modal('hide');
					serverTableForReject.draw(false);
				}
			});
		});
var submitBtnQ=function(o){
	var data = {
			"id" : o.id
		}
		$.ajax({
			url : rootPath + "/ui/eventRegister/submitEventRegister",
			dataType : 'json',
			data : JSON.stringify(data),
			type : 'post',
			contentType : "application/json",
			success : function(result) {
				if (result.data.code <= 0) {
					message("操作失败!");
				} else {
					message(result.data.submitStr);
				}
				serverTableForInit.draw(false);
			}
		});
};

// 点击完结按钮完结事件上报
$(".finishBtn").click(function() {
	var data ={
			"id":$("#identify").val(),
			"finishMemo":$("#finishMemo").val(),
			"reportEventAttrVariable":$("#reportEventAttrVariable").val()
	}
	$.ajax({
		url : rootPath + "/ui/eventReport/finishEventReport",
		dataType : 'json',
		data : JSON.stringify(data),
		type : 'post',
		contentType : "application/json",
		success : function(result) {
			if(result.data.code<=0){
        		message("完结失败！");
        	}
        	else{
        		message("完结成功！")
        	}
        	jQuery('#eventReportModal').modal('hide');
            serverTable.draw(false);
		}
	});
});
//点击受理事件上报按钮
$(".dealDetailBtn").click(function() {
	window.location = window.rootPath
	+ '/ui/eventRegister?eventReportIdentify='+$("#identify").val();
});

// 3 点击添加按钮弹出模态框
$(".addBtnLeft").click(function() {
	jQuery('#eventReportModal').modal('show');
});
// 1 初始化上报事件分页信息
function initEventPage() {

	serverTable = jQuery('#eventReportTable')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "get",
							"cache" : false,
							"url" : window.rootPath + '/ui/eventReport/getpage/'+"初始化",
							"data" : function(d) {
								return $.extend({}, d, serverData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : null,
									defaultContent : "",
									render : function(data, type, full, meta) {
										var operate = '<button type="button" class="btn btn-sm btn-info dealBtn" title="处理"><i class="fa fa-edit"></i></button>';
												/*+ '<button type="button" class="btn btn-sm btn-info rejBtn" title="拒绝"><i class="fa fa-remove"></i></button>'
												
												+ '<button type="button" class="btn btn-sm btn-danger finNode" title="完结"><i class="fa fa-unlock"></i></button>';*/
										return operate;
									}
								}, {
									data : 'reportEventType.eventName'
								}, {
									data : 'reportContent',
									render:function(data,type,full,meta){
					                	if(data == null || data == undefined || data == ""){
											return data;
										}else{
											return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
										}
									}
								}, {
									data : 'reportAccTime',
									render: function (data, type, full, meta) {
				                        if (data)
				                            return moment(data).format('YYYY/MM/DD HH:mm');
				                        else
				                            return '';
				                    }
								}, {
									data : 'reportUser.name'
								}, {
									data : 'reportAdress',
									render:function(data,type,full,meta){
					                	if(data == null || data == undefined || data == ""){
											return data;
										}else{
											return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
										}
									}
								}/*, {
									data : "reportImgAdress",
									render:function(data,type,full,meta){
					                	if(data == null || data == undefined || data == ""){
											return data;
										}else{
											return '<img width="50%" src="'+window.rootPath+"/"+data+'" />';
										}
									}

								}*/

						],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
							serverTable.off('click', 'tr');
							serverTable
									.on(
											'click',
											'tr',
											function(event) {
												event = event || window.event;
												var target = event.target
														|| event.scrElement;
												var data = serverTable
														.row(this).data();

												/*if (target.className
														.indexOf('fa-remove') != -1
														|| target.className
																.indexOf('rejBtn') != -1) {
													// 拒绝
													deleteGridById(data);
												}*/

												if (target.className
														.indexOf('fa-edit') != -1
														|| target.className
																.indexOf('dealBtn') != -1) {
													// 3 编辑
													dealModule(data);
												}
												/*if (target.className
														.indexOf('fa-sign-in') != -1
														|| target.className
																.indexOf('addNode') != -1) {
													bindNode(data);
												}
												if (target.className
														.indexOf('fa-unlock') != -1
														|| target.className
																.indexOf('removeNode') != -1) {

													unbindNode(data);
												}*/
												return;
											});
						}
					});

};